﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;
using BigLotterySyndicate.Classes;

namespace BigLotterySyndicate.Secured.Draws
{
    public partial class Create : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!this.IsUserLoggedIn)
                return;

            this.PageTitle += "Manage draws | Add new draw";

            this.MasterPage.SetTopMenuTab(global::BigLotterySyndicate.Secured.Controls.TopNav.AdminNavSection.Draws);
        }

        protected void ButtonContinue_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            int id =0;

            if (SaveForm(out id))
            {
                InsertEventLogEntry("Draw [" + id.ToString() + "] created.");
                Redirect(String.Format("/Secured/Draws/Default.aspx?Ns=2&dId={0}", id));
            }
            else
            {

            }
        }

        private bool SaveForm(out int dId)
        {
            draw d = new draw();

            d.syndicateId = this.LoggedInUser.syndicateId;
            d.drawdate = DateTime.Parse(this.CalendarDrawDate.CalendarValue);
            d.drawdate.AddHours(20);
            d.drawdate.AddMinutes(30);
            d.pricePerLine = float.Parse(this.DropDownListPrice.SelectedValue);
            
            d.isActivated = true;
            d.status = draw.drawstatus.OPEN;
            d.extraFunds = 0;
            d.winnings = 0;

            d.type = this.ddlType.SelectedValue;


            d.freeDraw = this.chbIsBonus.Checked;
           
            bool result = this.BusinessFacade.DrawBL.InsertDraw(d);

            dId = d.id;

            return result;
        }
    }
}